package com.bbc.wigeth;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by Administrator on 2017/9/26.
 * 随着手指滑动的圆当抬起时做个爆炸效果
 */

public class MyDrawCircleView extends View {
    //设置圆的位置
    private int X = 100;
    private int Y = 100;
    private Context mContext;

    private Paint mPaint;

    public MyDrawCircleView(Context context) {
        super(context, null);
        this.mContext = context;
    }

    public MyDrawCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制圆
        canvas.drawCircle(X, Y, 10, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                //获取当前触摸点的位置
                X = (int) event.getX();
                Y = (int) event.getY();
                Log.v("cbf", "X==" + X + " event.getX()==" + event.getX());
            case MotionEvent.ACTION_UP:
                //得到最终
                break;
        }
        //获取屏幕的宽高
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
//        Log.v("cbf", "width==" + width + " height==" + height);
        //限制只可以在屏幕内
        if (X >= 20 && Y >= 20 && X <= width - 20 && Y <= height - 90) {
            invalidate();
        }
        return true;
    }
}
